package com.shenwei.yingdaoguanli.service;

/**
 * @Author wang
 * @Date 2025/2/18 11:35
 * @PackageName:com.shenwei.yingdaoguanli.service
 * @ClassName: EmailService
 * @Description: TODO
 * @Version 1.0
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    public boolean sendEmailWithAttachment(String toEmail, String subject, String body, String filePath) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            // 设置邮件信息
            helper.setTo(toEmail);
            helper.setSubject(subject);
            helper.setText(body);
            helper.addAttachment("data.zip", new File(filePath)); // 添加附件

            // 发送邮件
            mailSender.send(message);
            return true;  // 发送成功
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;  // 发送失败
        }
    }
}

